package cn.haut.online_read.mapper;

import cn.haut.online_read.enrity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author xing
 * @date 2021-05-08 16:44
 */
@Mapper
public interface UserMapper {

    /**
     *  验证用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 返回信息，存在为 1 (普通用户) 或 2 (管理员) 不存在返回 null
     */
    @Select("SELECT permission FROM user WHERE username = #{username} AND password = #{password}")
    Integer validateUser(String username, String password);

    /**
     *
     * @param username
     * @param password
     * @return
     */
    @Insert("INSERT INTO user (username, password) VALUES (#{username}, #{password})")
    Boolean registerUser(String username, String password);

    /**
     * 获取用户的密码
     *
     * @param username
     * @return
     */
    @Select("SELECT * FROM user WHERE username = #{username}")
    User getUserByName(String username);

    /**
     * 修改密码
     *
     * @param username
     * @param newPassword
     * @return
     */
    @Update("UPDATE user SET password = #{newPassword} WHERE username = #{username}")
    Integer setPassword(String username, String newPassword);

    /**
     * 获取所有普通用户信息
     *
     * @return
     */
//    @Select("SELECT username FROM user WHERE permission = 1")
    @Select("SELECT username FROM user")
    List<String> getAllUsers();

    /**
     * 删除用户
     *
     * @param username
     * @return
     */
    @Delete("DELETE FROM user WHERE username = #{username}")
    Integer deleteUser(String username);

    /**
     * 删除用户有关的所有书架信息
     *
     * @param username
     * @return
     */
    @Delete("DELETE FROM bookcase WHERE user_id = #{username}")
    Integer deleteUserShelf(String username);

    /**
     * 通过用户名查找用户
     *
     * @param searchUsername
     * @return
     */
    @Select("SELECT username FROM user WHERE username LIKE CONCAT('%', ${username}, '%')")
    List<String> selectUserByUsername(String searchUsername);

    /**
     * 获取所有用户总数
     *
     * @return
     */
    @Select("Select COUNT(*) FROM user")
    int getAllUsersCount();

    /**
     * 根据分页获取用户信息
     *
     * @param offset
     * @param pageSize
     * @return
     */
    @Select("SELECT username FROM user LIMIT #{offset}, #{pageSize}")
    List<String> getUsersByPagination(int offset, int pageSize);

    /**
     * 获取用户信息 通过 用户名 近似查找， 以及 分页
     *
     * @param searchUsername
     * @param offset
     * @param pageSize
     * @return
     */
    @Select("SELECT username FROM user WHERE username LIKE CONCAT('%', ${searchUsername}, '%') LIMIT #{offset}, #{pageSize}")
    List<String> searchUsersByPagination(String searchUsername, int offset, int pageSize);

    /**
     * 获取近似查询到的所有用户的数量
     *
     * @param searchUsername
     * @return
     */
    @Select("SELECT COUNT(*) FROM user WHERE username LIKE CONCAT('%', ${searchUsername}, '%')")
    int searchUsersCount(String searchUsername);
}
